<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>react</title>
</head>
<body>
  <div id="root"></div>

  <script type="text/javascript" src="./components/react.development.js"></script>
  <script type="text/javascript" src="./components/react-dom.development.js"></script>
  <script type="text/javascript" src="./components/babel.min.js"></script>

  <script type="text/babel">
    // 封装真正可复用的 Clock 组件
    class Clock extends React.Component {

      // 添加一个class构造函数，然后再该函数中为this.state赋初值:
      constructor (props) {
        super(props);
        this.state = { date: new Date() };
      }


      // 生命周期方法

      // 挂载 mount
      // componentDidmount() 方法会在组件已经被渲染到DOM中后运行，所以，最好在这里设置计时器：
      componentDidMount () {
        this.timerID = setInterval(() => {
          this.tick()
        }, 1000);
      }
      // 卸载 unmount
      componentWillUnmount () {
        // 清除计时器
        clearInterval(this.timerID);
      }

      // 调计时器
      tick () {
        this.setState({
          date: new Date()
        })
      }


      render () {
        return (
          <div>
            <h1>Hello,world!</h1>
            <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
          </div>
        );
      }
    }

    ReactDOM.render(
      <Clock />,
      document.getElementById('root')
    )
    
  </script>
</body>
</html>